﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Text.RegularExpressions;
namespace Home.Utils
{
    public class StringUltility
    {
        public static string RemoveTag(string htmlText)
        {
            return Regex.Replace(htmlText, "<.*?>", "");
        }
        public static string GetExcerpt(string html, int excerptLen)
        {
            string plainText = RemoveTag(html);
            if (plainText.Length <= excerptLen) return plainText;
            return plainText.Substring(0, excerptLen);
        }
        public static SortedList<int, SortedList<string, int>> IndexComments(List<NhanAi.Entity.Comment> list)
        {

            //object[] indice = new object[list.Count];
            SortedList<int, SortedList<string, int>> indice = new SortedList<int, SortedList<string, int>>(list.Count);
            int parent = list[0].ParentId;
            SortedList<string, int> value = null;
            indice.TryGetValue(parent, out value);
            value.Add("start", 0);
            indice.Add(parent, value);
            for (int i = 0; i < list.Count; i++)
            {
                if (parent != list[i].ParentId)
                {
                    int oldParent;
                    SortedList<string, int> tmp = new SortedList<string, int>();
                    indice.TryGetValue(parent, out tmp);
                    tmp.Add("end", i - 1);
                    //$indice[$parent]['end'] = $i - 1; //mark end
                    oldParent = parent;

                    parent = list[i].ParentId;

                    //$parent = $comments[$i]->getCommentParent();
                    //$indice[$parent]['start'] = $i; //mark start
                    tmp.Add("start", i);
                    indice.Add(oldParent, tmp);
                } if (i == list.Count - 1)
                {
                    SortedList<string, int> tmp = new SortedList<string, int>();
                    indice.TryGetValue(parent, out tmp);
                    tmp.Add("end", i);
                    indice.Add(parent, tmp);
                    //$indice[$parent]['end'] = $i; //mark end
                }
            }
            return indice;
        }
        public static string PassParam(string formatString, object param)
        {
            return String.Format(formatString, param);
        }
    }

}